package com.wangjiyuan.giftshuo.ui.fragment.classify.strategy;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.ColumnBean;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.GroupBean;
import com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.column_details.all_details.ColumnDetailsActivity;
import com.wangjiyuan.giftshuo.ui.fragment.classify.strategy.adapter.StrategyRecyclerViewAdapter;
import com.wangjiyuan.giftshuo.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * 攻略的界面
 */
public class StrategyFragment extends Fragment
        implements StrategyContract.IStrategyView ,
        StrategyRecyclerViewAdapter.IColumnPositionListener {
    private Context mContext;
    private RecyclerView mRecyclerView;
    private List<Object> list;
    private StrategyRecyclerViewAdapter adapter;
    private LinearLayoutManager linearLayoutManager;

    private ColumnBean mColumnBean;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = getContext();
        list = new ArrayList<>();
        mColumnBean=new ColumnBean();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrategyPresenter strategyPresenter = new StrategyPresenter(this);
        strategyPresenter.getColumnObservable();
        strategyPresenter.getGroupObservable();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_strategy, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_strategy);
        linearLayoutManager = new LinearLayoutManager(mContext, OrientationHelper.VERTICAL, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, OrientationHelper.VERTICAL));
        mRecyclerView.setLayoutManager(linearLayoutManager);
//        adapter = new StrategyRecyclerViewAdapter(mContext, list, mColumnBean);
//        mRecyclerView.setAdapter(adapter);
    }
    /**
     * 实现view接口方法
     */
    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void errorInfo() {

    }

    @Override
    public void loadGroupBean(GroupBean groupBean) {
        List<GroupBean.DataBean.ChannelGroupsBean> groups = groupBean.getData().getChannelGroups();
        list.addAll(groups);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadColumnBean(ColumnBean columnBean) {
        mColumnBean=columnBean;
        list.add(0,columnBean);
        adapter = new StrategyRecyclerViewAdapter(mContext, list, mColumnBean,this);
        adapter.notifyDataSetChanged();
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    public void getColumnPosition(int position) {
        Intent intent=new Intent(mContext, ColumnDetailsActivity.class);
        startActivity(intent);
    }
}
